package com.lrkj.payfee.service.impl;

import java.util.List;

import com.lrkj.common.constant.FeeConstants;
import com.lrkj.common.constant.OrgConstants;
import com.lrkj.common.constant.PropertyConstants;
import com.lrkj.common.core.domain.entity.OrgTree;
import com.lrkj.common.exception.BaseException;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.SecurityUtils;
import com.lrkj.common.utils.uuid.IdUtils;
import com.lrkj.framework.utils.NumberUtils;
import com.lrkj.framework.utils.SerialNumUtil;
import com.lrkj.payfee.domain.MeterBox;
import com.lrkj.property.domain.Community;
import com.lrkj.property.domain.Room;
import com.lrkj.property.service.IRoomService;
import com.lrkj.system.service.IOrgTreeService;
import com.lrkj.waitress.domain.CustomInfo;
import com.lrkj.waitress.domain.JoinRegister;
import com.lrkj.waitress.service.IJoinRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lrkj.payfee.mapper.LeasePactMapper;
import com.lrkj.payfee.domain.LeasePact;
import com.lrkj.payfee.service.ILeasePactService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 租赁合同Service业务层处理
 *
 * @author yajun_ren
 * @date 2022-08-11
 */
@Service
public class LeasePactServiceImpl implements ILeasePactService
{
    @Autowired
    private LeasePactMapper leasePactMapper;


    @Autowired
    private IRoomService roomService;

    @Autowired IJoinRegisterService joinRegisterService;

    /**
     * 查询租赁合同
     *
     * @param id 租赁合同ID
     * @return 租赁合同
     */
    @Override
    public LeasePact selectLeasePactById(String id)
    {
        return leasePactMapper.selectLeasePactById(id);
    }

    /**
     * 查询租赁合同列表
     *
     * @param leasePact 租赁合同
     * @return 租赁合同
     */
    @Override
    public List<LeasePact> selectLeasePactList(LeasePact leasePact)
    {
        return leasePactMapper.selectLeasePactList(leasePact);
    }

    /**
     * 新增租赁合同
     *
     * @param leasePact 租赁合同
     * @return 结果
     */
    @Override
    public int insertLeasePact(LeasePact leasePact)
    {
        leasePact.setId(IdUtils.simpleUUID());
        leasePact.setCreateBy(SecurityUtils.getUsername());
        leasePact.setCreateTime(DateUtils.getNowDate());
        leasePact.setPactNo(this.getNo(FeeConstants.temp_lease_key));
        this.setInfo(leasePact);
        this.JoinRegistrer(leasePact);
        return leasePactMapper.insertLeasePact(leasePact);
    }

    /**
     * 修改租赁合同
     *
     * @param leasePact 租赁合同
     * @return 结果
     */
    @Override
    public int updateLeasePact(LeasePact leasePact)
    {
        leasePact.setUpdateBy(SecurityUtils.getUsername());
        leasePact.setUpdateTime(DateUtils.getNowDate());
        this.setInfo(leasePact);
        return leasePactMapper.updateLeasePact(leasePact);
    }

    /**
     * 批量删除租赁合同
     *
     * @param ids 需要删除的租赁合同ID
     * @return 结果
     */
    @Override
    public int deleteLeasePactByIds(String[] ids)
    {
        return leasePactMapper.deleteLeasePactByIds(ids);
    }

    /**
     * 删除租赁合同信息
     *
     * @param id 租赁合同ID
     * @return 结果
     */
    @Override
    public int deleteLeasePactById(String id)
    {
        return leasePactMapper.deleteLeasePactById(id);
    }
    /**
     * 批量新增租赁合同信息
     *
     * @param leasePacts 租赁合同对象
     */
    public int batchAddData(List<LeasePact> leasePacts)
    {
           return leasePactMapper.batchAddData(leasePacts);
    }


    public String getNo(String key) {
        int nextVal = SerialNumUtil.getInstance().nextInt(key);
        return key + NumberUtils.frontCompWithZore(nextVal, 7);
    }


    private void setInfo(LeasePact leasePact) {
        Room roomById = roomService.selectRoomById(leasePact.getRoomId());
        leasePact.setAncestors(roomById.getAncestors() + "," + leasePact.getId());
    }


    @Transactional
    public void JoinRegistrer(LeasePact x){
        Room roomById = roomService.selectRoomById(x.getRoomId());
        if (roomById.getOwnerId()!=null){
            throw new BaseException("该房屋已绑定业主！");
        }
        JoinRegister joinRegister=new JoinRegister();
        joinRegister.setCommunityId(x.getCommunityId());
        joinRegister.setFloorId(x.getFloorId());
        joinRegister.setUnitId(x.getUnitId());
        joinRegister.setRoomId(x.getRoomId());
        joinRegister.setCustomId(x.getPartyPerson());
        joinRegister.setStatus(PropertyConstants.join_register_20);
        joinRegisterService.insertJoinRegister(joinRegister);
    }
}
